/**
 *	A program when exit prints cpu useinfo 
 *	book: unix system programming
 *  page: 37	
 *	Sat Oct 28 01:46:11 CST 2023
 * */


#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/times.h>

static void showtimes (void)
{
	double ticks;
	struct tms tinfo;

	if ((ticks = (double) sysconf(_SC_CLK_TCK)) == -1)
		perror("Failed to determine clock ticks per second");
	else if (times (&tinfo) == (clock_t) - 1)
		perror("Failed to get times information");

	else{
	
		fprintf(stderr, "User time :	%8.3f seconds\n", 
				tinfo.tms_utime/ticks);
		fprintf(stderr, "System time:	%8.3f seconds\n", 
				tinfo.tms_stime/ticks);
		fprintf(stderr, "Children's user time:	%8.3f seconds\n", 
				tinfo.tms_cutime/ticks);
		fprintf(stderr, "Children's system time:	%8.3f seconds\n", 
				tinfo.tms_cstime/ticks);

	}
}

/** print user's id and group_id with real and effective  
 * page 42
 * */
static void prtids(void)
{
	printf("Real uid is :				%5ld\n",  (long) getuid());
	printf("effective uid  is :			%5ld\n",  (long) geteuid());
	printf("Real groupid is :			%5ld\n",  (long) getgid());
	printf("effective groupid is :		%5ld\n",  (long) getegid());
	printf("parent id is				%5ld\n",  (long) getppid());
}

int main(void) 
{
	prtids();
	if (atexit(showtimes)){
		fprintf(stderr, "Failed to install showtimes exit handler\n");
		return 1;

	}
	return 1;
}


